package cn.tedu.charging.device.service;

import cn.tedu.charging.common.pojo.po.ChargingGunInfoPO;
import cn.tedu.charging.common.pojo.po.ChargingStationPO;
import cn.tedu.charging.common.pojo.vo.GunInfoVO;
import cn.tedu.charging.common.pojo.vo.StationDetailVO;
import cn.tedu.charging.common.pojo.vo.StationInfoVO;
import cn.tedu.charging.device.dao.repository.GunRepository;
import cn.tedu.charging.device.dao.repository.StationRepository;
import com.alibaba.nacos.common.utils.CollectionUtils;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.GeoResult;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.redis.domain.geo.GeoLocation;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StationService {
    @Autowired
    private StationRepository stationRepository;
    @Autowired
    private GunRepository gunRepository;
    public List<StationInfoVO> nearbyStation(Double longitude, Double latitude, Double radius) {
        GeoResults<GeoLocation<Integer>> geoResults = stationRepository.nearbyStations(longitude,latitude,radius);
        List<GeoResult<GeoLocation<Integer>>> contents= geoResults.getContent();
        List<StationInfoVO> vos = null;
        if (CollectionUtils.isNotEmpty(contents)){
            log.debug("查询到{}个站点",contents.size());
            vos=new ArrayList<>();
            for (GeoResult<GeoLocation<Integer>> content : contents) {
                StationInfoVO vo = new StationInfoVO();
                vo.setStationId(content.getContent().getName());
                vo.setStationLng(new BigDecimal(content.getContent().getPoint().getX()));
                vo.setStationLat(new BigDecimal(content.getContent().getPoint().getY()));
                vo.setDistance(content.getDistance().getValue());
                String stationName = stationRepository.getStationNameById(vo.getStationId());
                vo.setStationName(stationName);
                vos.add(vo);
            }
        }
        return vos;
    }

    public StationDetailVO StationDetail(Integer stationId) {
        ChargingStationPO stationPO = stationRepository.getStationById(stationId);
        if (stationPO==null){
            throw new RuntimeException("没有找到对应的充电站");
        }
        StationDetailVO vo = new StationDetailVO();
        vo.setStationName(stationPO.getStationName());
        vo.setAddress(stationPO.getAddress());
        List<ChargingGunInfoPO> gunPos = gunRepository.getGunsByStationId(stationId);
        List<GunInfoVO> gunInfoVos = null;
        if (CollectionUtils.isNotEmpty(gunPos)){
            /*gunInfoVos=new ArrayList<>();
            for (ChargingGunInfoPO gunPO : gunPos) {
                GunInfoVO gunInfoVO = new GunInfoVO();
                gunInfoVO.setGunName(gunPO.getGunNumber());
                gunInfoVO.setGunStatus(gunPO.getGunStatus());
                gunInfoVO.setGunType(gunPO.getGunType());
                gunInfoVO.setElectricity(gunPO.getElectricity());
            }*/
            gunInfoVos = gunPos.stream().map(po -> {
                GunInfoVO gunInfoVO = new GunInfoVO();
                BeanUtils.copyProperties(po,gunInfoVO);
                return gunInfoVO;
            }).collect(Collectors.toList());
        }
        vo.setGunInfoPOList(gunInfoVos);
        return vo;
    }
}
